﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace Jabr.Logic.Roster
{
    [XmlRoot(ElementName = "roster")]
    public class RosterSingleton
    {
        static private RosterSingleton s_instance = null;
        static private readonly object s_padlock = new object();

        private List<AbstractAccount> m_acc;

        // Fuckin' serilization requirement :|
        public RosterSingleton()
        {
            this.m_acc = new List<AbstractAccount>();
        }

        [XmlIgnore]
        public static RosterSingleton Instance
        {
            get
            {
                lock (RosterSingleton.s_padlock)
                {
                    if (RosterSingleton.s_instance == null)
                    {
                        RosterSingleton.s_instance = new RosterSingleton();
                    }
                    return RosterSingleton.s_instance;
                }
            }
            set
            {
                lock (RosterSingleton.s_padlock)
                {
                    RosterSingleton.s_instance = value;
                }
            }
        }

        [
        XmlElement(ElementName = "abstract-acc", Type = typeof(AbstractAccount), IsNullable = true),
        XmlElement(ElementName = "jabber-acc", Type = typeof(JabberAccount), IsNullable = true)
        ]
        public List<AbstractAccount> AccountCollection
        {
            get
            {
                return this.m_acc;
            }
            set
            {
                this.m_acc = value;
            }
        }

        public delegate void AccountDelegate(AbstractAccount aa);

        public event AccountDelegate OnAdd;
        public void Add(AbstractAccount account)
        {
            if (this.OnAdd != null)
                this.OnAdd(account);
            this.m_acc.Add(account);
        }

        public event AccountDelegate OnDelete;
        public void Delete(AbstractAccount account)
        {
            if (this.OnDelete != null)
                this.OnDelete(account);
            this.m_acc.Remove(account);
        }
    }
}
